/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gen.seam.generator.template;

import static br.gen.seam.util.StringUtil.lowerFirstCharacter;
import br.gen.seam.util.entity.Column;
import java.util.List;

/**
 *
 * @author Gustavo
 */
public class PageTemplate extends Template {

    private StringBuffer page = new StringBuffer();
    private String beanName, entityName;

    private void instatiateVariables() {
        entityName = lowerFirstCharacter(getTable().getName());
        beanName = entityName + "Bean";
    }

    @Override
    public void generate() {
        instatiateVariables();
        head();
        genOutputs();
        genInputs();
    }

    private void head() {
        page.append("<ui:composition xmlns=\"http://www.w3.org/1999/xhtml\"\n")
            .append("xmlns:ui=\"http://java.sun.com/jsf/facelets\"\n")
            .append("xmlns:h=\"http://java.sun.com/jsf/html\"\n")
            .append("xmlns:f=\"http://java.sun.com/jsf/core\"\n")
            .append("xmlns:r=\"http://richfaces.org/rich\" xmlns:a=\"http://richfaces.org/a4j\"\n")
            .append("xmlns:s=\"http://jboss.com/products/seam/taglib\">\n");
    }

    private void genInputs() {
        for (Column column : getTable().getColumns()) {
            if (column.getType().isPrimitive()) {
                genInput(column);
            } else if (column.getType().isPrimitive() && column.getLength() == 1) {
                genCheck(column);
            } else {
                genSelectItem(column);
            }
        }
    }

    private void genInput(Column column) {
        page.append("<h:inputText " + getValue(column))
            .append(" id=\"" + lowerFirstCharacter(column) + "\"")
            .append(column.isNullable() ? "" : " required=\"true\"")
            .append(getLabel(column) + "/>");
    }

    private void genCheck(Column column) {
        page.append("<h:selectBooleanCheckbox value=\"#{" + beanName + "." + entityName + "." + lowerFirstCharacter(column) + "}\" >\n");
    }

    private void genSelectItem(Column column) {
        page.append("<h:selectOneMenu")
            .append(column.isNullable() ? "" : " required=\"true\"")
            .append("label=\"#{messages['tipo_evolucao_indice']}\"")
            .append(getValue(column))
            .append("<s:selectItems var=\"var\" value=\"#{" + beanName + ".lista}\"")
            .append("noSelectionLabel=\"Selecione\" itemValue=\"#{var.id}\"")
            .append(getLabel(column))
            .append("/> </h:selectOneMenu>\n");
    }

    private void genOutputs() {
        for (Column column : getTable().getColumns()) {
            String output = genOutputLabel(column);
            if (!column.isNullable()) {
                genRequired(output);
            }
        }
    }

    private String genOutputLabel(Column column) {
        return "<h:outputLabel value=\"#{messages['" + column + "']}\" />\n";
    }

    private void genRequired(String output) {
        page.append("<s:decorate template=\"/template/requiredLabel.xhtml\">").append(output).append("</s:decorate>");
    }

    private void genTable(List<Column> columns) {
        page.append("<r:dataTable id=\"tblIndices\" var=\"row\" value=\"#{indiceBean.indices}\" rows=\"50\" width=\"100%\" rowKeyVar=\"indice\" rowClasses=\"row-odd,row-even\">\n")
            .append("<f:facet name=\"footer\">\n")
            .append("<r:datascroller immediate=\"true\" maxPages=\"10\" status=\"_stsLoad1\" reRender=\"tblComponentesGrpCons\" />\n")
            .append("</f:facet>\n");

        for (Column column : columns) {
            page.append(genTableColumns(column));
        }

        page.append(genTableEdit())
            .append("Lack the delete button")
            .append("</r:dataTable>");
    }

    private String genTableColumns(Column column) {
        return "<r:column>\n"
                + "<f:facet name=\"header\">\n"
                + "<h:outputText value=\"#{messages['" + column + "']}\" />\n"
                + "</f:facet>\n"
                + "<h:outputText " + getValue(column) + " />\n"
                + "</r:column>\n";
    }

    private String genTableEdit() {
        return "<r:column>"
                + "<f:facet name=\"header\">"
                + "<h:outputText value=\"Editar\" />"
                + "</f:facet>"
                + "<a:commandButton value=\"Editar\" action=\"#{"+beanName+".editar(row)}\""
                + "ajaxSingle=\"true\" image=\"/img/button/BtnGridEdit.gif\" reRender=\"cadIndice\"/>"
                + "</r:column>";
    }

    private String getValue(Column column) {
        return " value=\"#{" + beanName + "." + entityName + "." + lowerFirstCharacter(column) + "}\" ";
    }

    private String getLabel(Column column) {
        return " label=\"#{messages['" + column + "']}\" ";
    }

    @Override
    public String toString() {
        return page.toString();
    }
}
